package com.ruoyi.heima.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.heima.domain.po.NewsType;
import com.ruoyi.heima.service.INewsTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 新闻类型
@RestController
// 请求路径
@RequestMapping("/newsType")
// 自动注入
@RequiredArgsConstructor

public class NewsTypeController extends BaseController {

    // 自动注入
    private final INewsTypeService newsTypeService;

    /**
     * 查询新闻类型列表
     * @param newsType
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(NewsType newsType) {
        // 调用若依的方法，进行分页
        startPage();
        List<NewsType> newsTypes = newsTypeService.selectNewsTypeList(newsType);
        return getDataTable(newsTypes);

    }


    /**
     * 新增NewsType
     */
    @RequiresPermissions("heima:newsType:add")
    @Log(title = "NewsType", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NewsType newsType)
    {
        return toAjax(newsTypeService.insertNewsType(newsType));
    }

    /**
     * 根据id获取NewsType详细信息
     */
    @RequiresPermissions("heima:newsType:query")
    @GetMapping(value = "/{newsTypeId}")
    public AjaxResult getInfo(@PathVariable("newsTypeId") Long newsTypeId)
    {
        return success(newsTypeService.selectNewsTypeByTypeId(newsTypeId));
    }

    /**
     * 修改NewsType
     */
    @RequiresPermissions("heima:newsType:edit")
    @Log(title = "NewsType", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NewsType newsType)
    {
        return toAjax(newsTypeService.updateNewsType(newsType));
    }

    /**
     * 删除NewsType
     */
    @RequiresPermissions("heima:newsType:remove")
    @Log(title = "NewsType", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable List<Long> typeIds)
    {
        return toAjax(newsTypeService.deleteNewsTypeByTypeIds(typeIds));
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public List<NewsType> all() {
        List<NewsType> List = newsTypeService.getAll();
        return List;
    }
}



